const Sequelize = require('sequelize');
let db = require('../../config/lib/db');
const Role = require('./Role');
const Department = require('./Department');

let User = db.define('user', {
    id: {
        filed: 'id',
        primaryKey: true,
        type: Sequelize.BIGINT,
        autoIncrement: true,
        allowNull: false
    },
    name: {
        field: 'name',
        type: Sequelize.STRING,
        allowNull: false
    },
    password: {
        field: 'password',
        type: Sequelize.STRING,
        allowNull: false
    },
    role_id: {
        field: 'role_id',
        type: Sequelize.TINYINT,
        defaultValue: 1, // 0管理员【仅限admin】 1程序员
        allowNull: false
    },
    part_id: {
        field: 'part_id',
        type: Sequelize.TINYINT
    },
    permission: {
        field: 'permission',
        type: Sequelize.STRING
    },
    status: {
        field: 'status',
        type: Sequelize.TINYINT,
        defaultValue: 0, // 0正常 1禁用
        allowNull: false
    },
    ip: {
        field: 'ip',
        type: Sequelize.STRING
    }
},{
    tableName: 't_user',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    // paranoid: false, // 删除数据时不删除数据而是更新deleteAt字段,如果需要设置为true，则上面的deleteAt字段不能为false
    // 锁定表名，!+s
    freezeTableName: true,
    charset: 'utf8mb4',
    collate: 'utf8mb4_general_ci'
});

User.belongsTo(Role, {foreignKey: 'role_id', targetKey: 'id', constraints: false})
User.belongsTo(Department, {foreignKey: 'part_id', targetKey: 'id', constraints: false})

User.sync({ force: false, alter: false });

module.exports = User;
